package org.ilead.iserve.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.ilead.iserve.dao.IUsersDAO;
import org.ilead.iserve.model.Users;
import org.ilead.iserve.service.IUsersService;

public class UsersService implements IUsersService {
	private static final Logger log = LoggerFactory
			.getLogger(UsersService.class);
	private IUsersDAO usersDAO = null;

	public void setUsersDAO(IUsersDAO usersDAO) {
		this.usersDAO = usersDAO;
	}

	public void addUsers(Users u) throws Exception {
		usersDAO.save(u);
	}

	public void delUsers(Integer uid) throws Exception {
		usersDAO.delete(findByUid(uid));
	}

	public List<Users> findAll() {
		return usersDAO.findAll();
	}

	public List<Users> findByName(String uname) {
		return usersDAO.findByUname(uname);
	}

	public Users findByUid(Integer uid) {
		return usersDAO.findById(uid);
	}

	public Users login(String uname, String upass) {

		List list = usersDAO.findByUname(uname);
		if (null == list || list.size() == 0)
			return null;
		Users u = (Users) usersDAO.findByUname(uname).get(0);
		if (null == u)
			return null;
		if (uname.equals(u.getUname()) && upass.equals(u.getUpass()))
			return u;
		else
			return null;
	}

	public void updateUsers(Users u) throws Exception {
		usersDAO.attachDirty(u);
	}
}
